package cn.itsource.blog.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.itsource.blog.domain.LoginInfo;
import cn.itsource.blog.domain.Menu;
import cn.itsource.blog.domain.Permission;
import cn.itsource.blog.dto.LoginDto;
import cn.itsource.blog.dto.Payload;
import cn.itsource.blog.mapper.LogininfoMapper;
import cn.itsource.blog.mapper.MenuMapper;
import cn.itsource.blog.mapper.PermissionMapper;
import cn.itsource.blog.service.ILogininfoService;
import cn.itsource.blog.service.IMenuService;
import cn.itsource.blog.util.JwtUtil;
import cn.itsource.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LogininfoServiceImpl implements ILogininfoService {

    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private IMenuService menuService;
    @Override
    public String login(LoginDto loginDto) {
        if(StrUtil.isBlank(loginDto.getUsername())){
            throw new BusinessException("账号必填");
        }
        if (StrUtil.isBlank(loginDto.getPassword())) {
            throw new BusinessException("密码必填");
        }
        LoginInfo loginInfo = logininfoMapper.getByUsername(loginDto.getUsername());
        if(loginDto==null){
            throw new BusinessException("账号错误");
        }
        String password = SecureUtil.md5(loginInfo.getSalt() + loginDto.getPassword());
        if(!loginInfo.getPassword().equals(password)){
            throw new BusinessException("密码错误");
        }

        //权限
        List<Permission> permissions = permissionMapper.getAllByLoginId(loginInfo.getId());

        //菜单
        List<Menu> menus = menuMapper.getAllByLoginId(loginInfo.getId());
        menus= list2Tree(menus);

        Payload payLoader = new Payload(loginInfo, menus, permissions);

        String token = JwtUtil.generateToken(payLoader);

        return token;
    }
    public List<Menu> list2Tree(List<Menu> menus){
        //把List集合转为Map集合
        Map<Long, Menu> map = new HashMap<>();
        for (Menu menu : menus) {
            map.put(menu.getId(), menu);
        }


        List<Menu> newList = new ArrayList<>();

        for (Menu menu : menus) {
            if(menu.getParentId()==null){
                newList.add(menu);
            }else{
                Menu type = map.get(menu.getParentId());
                if(type!=null){
                    type.getChildren().add(menu);
                }

            }
        }

        return newList;
    }

    public static void main(String[] args) {
        String s = RandomUtil.randomString(32);
        System.out.println(s);

        String s1 = SecureUtil.md5("qxx3iqiacms8rrx39mkko07acy847ufh" + 123);
        System.out.println(s1);
    }
}
